<?php

namespace app\stores\controller\admin\cash;

use app\BaseController;
use app\beautiful\model\goods\GoodsGroupDetailModel;
use app\beautiful\model\goods\GoodsGroupModel;
use app\beautiful\model\goods\GoodsOtherSkuModel;
use app\beautiful\model\mall\GoodsModel;
use app\beautiful\model\mall\GoodsSkuModel;
use app\stores\model\cash\CashCategoryModel;
use Overtrue\Pinyin\Pinyin;
use think\facade\Db;

class Goods extends BaseController{
    /**
     * 显示菜品
     */
    public function show()

    {
        $goods_id = $this->request->param('goods_id/d',0);

        $where['goods_id'] = $goods_id;
        $where['origin_type'] = 2;
        $where['is_delete'] = 0;
        $where['shop_id'] = $this->request->shopId;

        $goods_model = new GoodsModel();

        $goods = $goods_model->getById($where);

        if(empty($goods)) return $this->error('菜品不存在');

        $other_sku_id = $goods['other_sku_id']?explode(',',$goods['other_sku_id']):$goods['other_sku_id'];

        if($other_sku_id)   $ph = GoodsOtherSkuModel::where(['type'=>1])->whereIn('other_sku_id',$other_sku_id)->field('other_sku_id,group_name')->select()->toArray();

        $goods['kw'] = $other_sku_id?$ph:[];

        if($other_sku_id)   $gy = GoodsOtherSkuModel::where(['type'=>2])->whereIn('other_sku_id',$other_sku_id)->field('other_sku_id,group_name')->select()->toArray();

        $goods['zf'] = $other_sku_id?$gy:[];

        $goods['show_type'] = $goods['show_type']?explode(',',$goods['show_type']):$goods['show_type'];

        if($goods['is_have_sku']){

            $sku = GoodsSkuModel::list(['goods_id'=>$goods_id,'shop_id'=>$this->request->shopId],['gs.add_time'=>'asc'],1,9999);

        }else{
            $sku[] = [
                'name'=>$goods['sku_name'],
                'crossed_price'=>$goods['crossed_price'],
                'gross'=>$goods['gross'],
                'unit_id'=>$goods['unit_id'],
                'discount_detail'=>$goods['discount_detail'],
            ];
        }
        $goods['sku'] = $sku;

        return $this->success($goods);

    }
    /**
     * 添加/修改菜品（收银无限量不判断库存）
     */
    public function save()

    {

        $goods_id = $this->request->param('goods_id/d',0);

        $type = $this->request->param('type/d',2);

        $name = $this->request->param('name');

        $cash_category_id = $this->request->param('cash_category_id/d',0);

        $produce_depart_id = $this->request->param('produce_depart_id/d',0);//默认选的收银菜品分类对应的出品部门,不匹配可以重新选

        $online_category_id = $this->request->param('online_category_id/d',0);

        $mnemonic_code = $this->request->param('mnemonic_code');//菜品助记码，不输入系统默认显示

        $status = $this->request->param('status/d',-1);//菜品售卖

        $online_status = $this->request->param('online_status/d',-1);//线上点餐

        $support_service_type = $this->request->param('support_service_type/a',[]);//支持业务：1堂食2打包3外卖

        $sku_detail = $this->request->param('sku');

        $other_sku_id = $this->request->param('other_sku_id/a',[]);//口味和做法

        $vip_discount_type = $this->request->param('vip_discount_type/d');//0无优惠1会员折扣2指定价格3减价

        $cover_img = $this->request->param('cover_img');//菜品图

        $goods_details = $this->request->param('goods_details/a',[]);

        $show_type = $this->request->param('show_type');

        $pungency_type = $this->request->param('pungency_type/d',1);

        $remind = $this->request->param('remind');

        $sell_time_type = $this->request->param('sell_time_type/d',0);

        $sell_start_time = $this->request->param('sell_start_time');

        $sell_end_time = $this->request->param('sell_end_time');

        $is_discount = $this->request->param('is_discount/d',0);

        $is_real_income = $this->request->param('is_real_income/d',0);

        $start_sales_num = $this->request->param('start_sales_num/d',0);

        $max_num = $this->request->param('max_num/d',-1);

        $unit_id = $this->request->param('unit_id/d',0);

        $crossed_price = $this->request->param('crossed_price/f',0);

        $gross = $this->request->param('gross/f',0);

        $origin_type = $this->request->param('origin_type/d',2);

        //判断传来的参数

        if(empty($name)) return $this->error('请输入名称');

        if(!in_array($type,[1,2,3])) return $this->error('type值不对');

        if($cash_category_id<1) return $this->error('请选择收银菜品分类');

        if($produce_depart_id<1) return $this->error('请选择出品部门');

        if($online_category_id<1) return $this->error('请选择线上菜品分类');

        if(empty($support_service_type)) return $this->error('支持业务必选');

        if(empty($cover_img)) return $this->error('菜品图必传');

        if(!in_array($vip_discount_type,[0,1,2,3])) return $this->error('未设置会员价类型');

        if($sell_time_type==1 && (empty($sell_start_time) || empty($sell_end_time))) return $this->error('售卖时间必传');

        if($start_sales_num<1) return $this->error('起售量不能小于1');

        if(!$sku_detail) return $this->error('至少一个规格');

        $cash_category = CashCategoryModel::info(['shop_id'=>$this->request->shopId,'status'=>1,'is_delete'=>0,'type'=>1,'category_id'=>$cash_category_id]);

        $online_cash_category = CashCategoryModel::info(['shop_id'=>$this->request->shopId,'status'=>1,'is_delete'=>0,'type'=>2,'category_id'=>$online_category_id]);

        if(empty($cash_category)) return $this->error('收银菜品分类不存在');

        if(empty($online_cash_category)) return $this->error('线上菜品分类不存在');

        if(empty($mnemonic_code)){

            $pinyin = new Pinyin();

            $mnemonic_code = strtoupper($pinyin->abbr($name));
        }

        if($sku_detail){

            foreach ($sku_detail as $k => $v){

                if(empty($v['name'])) return $this->error('请输入规格第'.($k+1).'行的名称');

                if(empty($v['unit_id'])) return $this->error('请选择规格第'.($k+1).'行的单位');

                if(empty($v['gross']) || $v['gross'] <= 0) return $this->error('请选择规格第'.($k+1).'行的产品毛利');

                if(empty($v['crossed_price']) || $v['crossed_price'] <= 0) return $this->error('请输入规格第'.($k+1).'行的销售价，且不能小于0');

                if($vip_discount_type>0 && empty($v['discount_detail']) ) return $this->error('请输入规格第'.($k+1).'行的会员价优惠');


            }

        }
        $goods_model = new GoodsModel();

        if($goods_id != 0){
            $where['shop_id'] = $this->request->shopId;
            $where['goods_id'] = $goods_id;
            $where['is_delete'] = 0;
            $where['origin_type'] = $origin_type;
            $goods = $goods_model->getById($where);
            if(empty($goods)) return $this->error('菜品不存在');
            $goods = $goods->toArray();
            $pre_cover_img=$goods['cover_img'];  $pre_goods_details=$goods_details;
        }else{
            $pre_cover_img=null; $pre_goods_details=[];
        }

        Db::startTrans();

        try{
            $goods['shop_id'] = $this->request->shopId;
            $goods['type'] = $type;
            $goods['name'] = $name;
            $goods['sku_name'] = $sku_detail[0]['name'];
            $goods['cash_category_id'] = $cash_category_id;
            $goods['produce_depart_id'] = $produce_depart_id;
            $goods['online_category_id'] = $online_category_id;
            $goods['mnemonic_code'] = $mnemonic_code;
            $goods['status'] = $status;
            $goods['online_status'] = $online_status;
            $goods['support_service_type'] = $support_service_type?implode(',',$support_service_type):null;
            $goods['unit_id'] = $sku_detail[0]['unit_id'];
            $goods['crossed_price'] = $sku_detail[0]['crossed_price'];
            $goods['gross'] = $sku_detail[0]['gross'];
            $goods['other_sku_id'] = $other_sku_id?implode(',',$other_sku_id):$other_sku_id;
            $goods['is_have_sku'] = count($sku_detail)>1?1:0;
            $goods['vip_discount_type'] = $vip_discount_type;
            $goods['discount_detail'] = $vip_discount_type>0?json_encode($sku_detail[0]['discount_detail'],true):null;
            $goods['cover_img'] = $cover_img;
            $goods['goods_details'] = json_encode($goods_details,true);
            $goods['show_type'] = $show_type?implode(',',$show_type):$show_type;
            $goods['pungency_type'] = $pungency_type;
            $goods['remind'] = $remind;
            $goods['sell_time_type'] = $sell_time_type;
            $goods['sell_start_time'] = $sell_start_time;
            $goods['sell_end_time'] = $sell_end_time;
            $goods['is_discount'] = $is_discount;
            $goods['is_real_income'] = $is_real_income;
            $goods['start_sales_num'] = $start_sales_num;
            $goods['max_num'] = $max_num;
            $goods['update_time'] = time();

            if($goods_id<1){
                $goods['quota'] = 9999;
                $goods['add_time'] = time();
                $goods['add_ip'] = request()->ip();
                $origin_type = [2,3];
                for($i=0;$i<count($origin_type);$i++){
                    unset($goods['goods_id']);
                    $goods['origin_type'] = $origin_type[$i];
                    $goods['goods_id'] = GoodsModel::insertGetId($goods);
                    if($sku_detail && $goods['is_have_sku']){
                        $this->sku($sku_detail, $goods);
                    }
                }
            }else{
                GoodsModel::up($goods,['goods_id'=>$goods_id]);
                //没有规格删除操作
                if($goods['is_have_sku']<1){
                    GoodsSkuModel::update(['is_delete'=>1],['goods_id'=>$goods['goods_id'],'shop_id'=>$this->request->shopId,'is_delete'=>0]);
                }
                if($sku_detail && $goods['is_have_sku']){
                    $this->sku($sku_detail,$goods);
                }
            }
            

            Db::commit();
        }catch(\Exception $e){

            Db::rollback();

            return $this->error($e->getMessage());

        }
        //图片操作
        imgReplaceAct($cover_img,$pre_cover_img);
        arrayNoUseImg($pre_goods_details);
        arrayUseImg($goods_details);

        return $this->success('操作成功');

    }

    function sku($sku_detail,$goods){

        $old_sku=GoodsSkuModel::where([

            ['shop_id','=',$this->request->shopId],

            ['goods_id','=',$goods['goods_id']],

            ['is_delete','=',0]

        ])->json(['discount_detail'],true)->select()->toArray();

        $update_goods_ids=array_column($sku_detail,'sku_id') ?? []; $delete_goods_sku=[];

        $old_sku_ids=array_column($old_sku,'sku_id') ?? [];

        foreach ($old_sku as $k => $v){

            if(!in_array($v['sku_id'],$update_goods_ids)){

                $v['is_delete']=1;

                $delete_goods_sku[]=$v;

            }

        }

        foreach ($sku_detail as $k => $v){

            $sku_detail[$k]['shop_id']=$this->request->shopId;

            $sku_detail[$k]['goods_id']=$goods['goods_id'];

            if(isset($v['sku_id'])){

                if(!in_array($v['sku_id'],$old_sku_ids)) unset($sku_detail[$k]);

            }
            $sku_detail[$k]['discount_detail']= $goods['vip_discount_type']>0?json_encode($v['discount_detail'],true):null;

        }
        $new_sku=array_merge($sku_detail,$delete_goods_sku);
        //删除

        $goods_sku_model= new GoodsSkuModel();

        $goods_sku_model->saveAll($new_sku);
    }
    /**
     *显示套餐
     */
    public function showSetMeal()

    {

        $goods_id = $this->request->param('goods_id/d',0);

        $where['goods_id'] = $goods_id;
        $where['origin_type'] = 2;
        $where['type'] = 3;
        $where['is_delete'] = 0;
        $where['shop_id'] = $this->request->shopId;
        $goods_model = new GoodsModel();
        $goods =$goods_model->getById($where);

        if(empty($goods)) return $this->error('套餐不存在');

        //分组
        $return = GoodsGroupModel::list(['goods_id'=>$goods_id],['add_time'=>'asc']);

        $goods['group'] = $return['list'];

        return $this->success($goods);

    }
    /**
     * 添加/修改套餐（收银无限量不判断库存）
     */
    public function saveSetMeal()

    {

        $goods_id = $this->request->param('goods_id/d',0);

        $name = $this->request->param('name');

        $unit_id = $this->request->param('unit_id/d',0);

        $crossed_price = $this->request->param('crossed_price/f',0);

        $gross = $this->request->param('gross');

        $total_value = $this->request->param('total_value');

        $type = $this->request->param('type/d',3);

        $cash_category_id = $this->request->param('cash_category_id/d',0);

        $online_category_id = $this->request->param('online_category_id/d',0);

        $mnemonic_code = $this->request->param('mnemonic_code');//菜品助记码，不输入系统默认显示

        $status = $this->request->param('status/d',-1);//菜品售卖

        $online_status = $this->request->param('online_status/d',-1);//线上点餐

        //$support_service_type = $this->request->param('support_service_type/a',[]);//支持业务：1堂食2打包3外卖

        $support_service_type[] = 1;

        $group_data = $this->request->param('group_data/a',[]);

        $vip_discount_type = $this->request->param('vip_discount_type/d');//0无优惠1会员折扣2指定价格3减价

        $discount_detail = $this->request->param('discount_detail/a',[]);

        $cover_img = $this->request->param('cover_img');//菜品图

        $goods_details = $this->request->param('goods_details/a',[]);

        $show_type = $this->request->param('show_type');

        $pungency_type = $this->request->param('pungency_type/d',1);

        $remind = $this->request->param('remind');

        $sell_time_type = $this->request->param('sell_time_type/d',0);

        $sell_start_time = $this->request->param('sell_start_time');

        $sell_end_time = $this->request->param('sell_end_time');

        $is_discount = $this->request->param('is_discount/d',0);

        $is_real_income = $this->request->param('is_real_income/d',0);

        $start_sales_num = $this->request->param('start_sales_num/d',0);

        $max_num = $this->request->param('max_num/d',-1);

        $origin_type = $this->request->param('origin_type/d',2);


        //判断传来的参数

        if(empty($name)) return $this->error('请输入名称');

        if($unit_id<1) return $this->error('请选择单位');

        if(!in_array($type,[1,2,3])) return $this->error('type值不对');

        if($cash_category_id<1) return $this->error('请选择收银菜品分类');

        if($online_category_id<1) return $this->error('请选择线上菜品分类');

        if(empty($support_service_type)) return $this->error('支持业务必选');

        if(empty($cover_img)) return $this->error('菜品图必传');

        if(!in_array($vip_discount_type,[0,1,2,3])) return $this->error('未设置会员价类型');

        if($sell_time_type==1 && (empty($sell_start_time) || empty($sell_end_time))) return $this->error('售卖时间必传');

        if($start_sales_num<1) return $this->error('起售量不能小于1');

        if(count($group_data)<1) return $this->error('至少一个分组');

        $cash_category = CashCategoryModel::info(['shop_id'=>$this->request->shopId,'status'=>1,'is_delete'=>0,'type'=>1,'category_id'=>$cash_category_id]);

        $online_cash_category = CashCategoryModel::info(['shop_id'=>$this->request->shopId,'status'=>1,'is_delete'=>0,'type'=>2,'category_id'=>$online_category_id]);

        if(empty($cash_category)) return $this->error('收银菜品分类不存在');

        if(empty($online_cash_category)) return $this->error('线上菜品分类不存在');

        if(empty($mnemonic_code)){

            $pinyin = new Pinyin();

            $mnemonic_code = strtoupper($pinyin->abbr($name));
        }

        $goods_model = new GoodsModel();

        if($goods_id != 0){
            $where['shop_id'] = $this->request->shopId;
            $where['goods_id'] = $goods_id;
            $where['is_delete'] =0;
            $where['origin_type'] =$origin_type;
            $goods = $goods_model->getById($where);
            if(empty($goods)) return $this->error('套餐不存在');
            $goods = $goods->toArray();
            $pre_cover_img=$goods['cover_img']; $pre_goods_details=$goods_details;
        }else{
            $pre_cover_img=null; $pre_goods_details=[];
        }

        Db::startTrans();

        try{
            $goods['shop_id'] = $this->request->shopId;
            $goods['type'] = $type;
            $goods['name'] = $name;
            $goods['unit_id'] = $unit_id;
            $goods['gross'] = $gross;
            $goods['crossed_price'] = $crossed_price;
            $goods['total_value'] = $total_value;
            $goods['cash_category_id'] = $cash_category_id;
            $goods['produce_depart_id'] = $cash_category['produce_depart_id'];;
            $goods['online_category_id'] = $online_category_id;
            $goods['mnemonic_code'] = $mnemonic_code;
            $goods['status'] = $status;
            $goods['online_status'] = $online_status;
            $goods['support_service_type'] = $support_service_type?implode(',',$support_service_type):null;
            $goods['vip_discount_type'] = $vip_discount_type;
            $goods['discount_detail'] = $vip_discount_type>0?json_encode($discount_detail,true):null;
            $goods['cover_img'] = $cover_img;
            $goods['goods_details'] = json_encode($goods_details,true);
            $goods['show_type'] = $show_type?implode(',',$show_type):$show_type;
            $goods['pungency_type'] = $pungency_type;
            $goods['remind'] = $remind;
            $goods['sell_time_type'] = $sell_time_type;
            $goods['sell_start_time'] = $sell_start_time;
            $goods['sell_end_time'] = $sell_end_time;
            $goods['is_discount'] = $is_discount;
            $goods['is_real_income'] = $is_real_income;
            $goods['start_sales_num'] = $start_sales_num;
            $goods['max_num'] = $max_num;
            $goods['update_time'] = time();
            $goods['origin_type'] = 2;

            if($goods_id<1){
                $goods['add_time'] = time();
                $goods['add_ip'] = request()->ip();
                $goods['goods_id'] = GoodsModel::insertGetId($goods);
            }else{
                GoodsModel::up($goods,['goods_id'=>$goods_id]);
            }

            $goods_group_model= new GoodsGroupModel();
            $goods_group_detail_model= new GoodsGroupDetailModel();

            if($goods_id){
                //分组和分组明细一起删
                GoodsGroupModel::up(['is_delete'=>1],['shop_id'=>$this->request->shopId,'goods_id'=>$goods_id]);
                GoodsGroupDetailModel::up(['is_delete'=>1],['shop_id'=>$this->request->shopId,'goods_id'=>$goods_id]);
            }

            foreach ($group_data as $key=>$value){
                $group=[
                    'shop_id'=>$this->request->shopId,
                    'goods_id'=>$goods['goods_id'],
                    'group_name'=>$value['group_name'],
                    'type'=>$value['type'],
                    'num'=>$value['num'],
                    'choose_num'=>$value['choose_num'],
                    'is_choose_same'=>$value['is_choose_same'],
                ];

                $goods_group_id = $goods_group_model->insertGetId($group);

                if($value['groupDetail']){
                    $goods_group_detail=[];
                    foreach ($value['groupDetail'] as $k=>$v){
                        $goods_group_detail[] = [
                            'shop_id'=>$this->request->shopId,
                            'goods_group_id'=>$goods_group_id,
                            'goods_id'=>$v['goods_id'],
                            'num'=>$v['num'],
                            'sort'=>$v['sort'],
                            'sku_id'=>$v['sku_id'],

                        ];
                    }
                    $goods_group_detail_model->saveAll($goods_group_detail);
                }

            }

            Db::commit();
        }catch(\Exception $e){

            Db::rollback();

            return $this->error($e->getMessage());

        }
        //图片操作
        imgReplaceAct($cover_img,$pre_cover_img);
        arrayNoUseImg($pre_goods_details);
        arrayUseImg($goods_details);

        return $this->success('操作成功');

    }
    /**
     * 菜品列表
     */
    public function list(){
        $search = $this->request->param('search');
        $origin_type = $this->request->param('origin_type/d',2);//2收银3线上
        $cash_category_id = $this->request->param('cash_category_id/d',0);
        $online_category_id = $this->request->param('online_category_id/d',0);

        if($search) $where['search'] = $search;
        if($cash_category_id>0) $where['cash_category_id'] = $cash_category_id;
        if($online_category_id>0) $where['online_category_id'] = $online_category_id;
        $where['origin_type'] = $origin_type;
        $where['shop_id'] = $this->request->shopId;

        $filed = 'g.type,g.is_have_sku,g.original_price,g.produce_depart_id,g.cash_category_id,g.online_category_id,g.vip_discount_type,g.fictitious_take_count,g.goods_id,g.shop_id,g.name,g.cash_category_id,g.online_category_id,g.status,g.online_status,g.crossed_price,g.gross,g.support_service_type,
        g.origin_type,g.add_time,g.discount_detail,sd.group_detail_name as unit_name';
        $return = GoodsModel::list($where,['add_time'=>'desc'],$this->request->page,$this->request->limit,$filed);
        return $this->success($return);
    }
    /**
     * 复制菜品
     */
    public function copy(){
        $goods_ids = $this->request->param('goods_ids/a',[]);

        $where['origin_type'] = 2;
        $where['is_delete'] = 0;
        $where['shop_id'] = $this->request->shopId;

        $list = GoodsModel::where($where)->whereIn('goods_id',$goods_ids)->select()->toArray()??[];

        if(empty($list)) return $this->error('菜品不存在');

        $have_goods_ids= array_column($list,'goods_id');

        for ($i=0;$i<count($goods_ids);$i++){
            if(!in_array($goods_ids[$i],$have_goods_ids)){
                return $this->error('菜品Id为'.$goods_ids[$i].'不存在');
            }
        }

        Db::startTrans();

        try{
            foreach ($list as $k1=>$v1){
                $goods = $v1;
                unset($goods['goods_id']);
                $copy_goods = $goods;
                $copy_goods['add_time'] = time();
                $copy_goods['update_time'] = time();
                $copy_goods['add_ip'] = request()->ip();

                $copy_goods['goods_id'] = GoodsModel::insertGetId($copy_goods);

                //复制产品
                if($copy_goods['type']==2 && $copy_goods['is_have_sku']){

                    $sku_detail=GoodsSkuModel::where([

                        ['shop_id','=',$this->request->shopId],

                        ['goods_id','=',$v1['goods_id']],

                        ['is_delete','=',0]

                    ])->select()->toArray();

                    $copy_sku=[];
                    foreach ($sku_detail as $k => $v){
                        unset($v['sku_id']);
                        $copy_sku[$k] = $v;
                        $copy_sku[$k]['goods_id']=$copy_goods['goods_id'];
                        $copy_sku[$k]['add_time'] = time();
                        $copy_sku[$k]['add_ip'] = request()->ip();

                    }
                    $goods_sku_model= new GoodsSkuModel();
                    $goods_sku_model->saveAll($copy_sku);


                }

                //复制套餐
                if($copy_goods['type']==3){
                    $goods_group_model = new GoodsGroupModel();
                    $goods_group_detail_model = new GoodsGroupDetailModel();
                    $group_data = $goods_group_model->where(['shop_id'=>$this->request->shopId,'goods_id'=>$v1['goods_id'],'is_delete'=>0])->select();
                    foreach ($group_data as $key=>$value){
                        $copy_group_data = $value;
                        unset($copy_group_data['goods_group_id']);
                        $copy_group_data['goods_id']=$copy_goods['goods_id'];
                        $copy_group_data['add_time'] = date('Y-m-d H:i:s');
                        $copy_group_data['add_ip'] = request()->ip();

                        $goods_group_id = $goods_group_model->insertGetId($copy_group_data);

                        $group_detail = $goods_group_detail_model->where(['goods_group_id'=>$value['goods_group_id'],'is_delete'=>0])->select();
                        if($group_detail){
                            $copy_group_detail=[];
                            foreach ($group_detail as $k=>$v){
                                unset($v['goods_group_detail_id']);
                                $copy_group_detail[$k] = $v;
                                $copy_group_detail[$k]['goods_group_id'] = $goods_group_id;
                                $copy_group_detail[$k]['add_time'] = date('Y-m-d H:i:s');
                                $copy_group_detail[$k]['add_ip'] = request()->ip();
                            }
                            $goods_group_detail_model->saveAll($copy_group_detail);
                        }

                    }


                }
            }

            Db::commit();
        }catch(\Exception $e){

            Db::rollback();

            return $this->error($e->getMessage());

        }
        return $this->success('复制成功');
    }
    /**
     * 启用/禁用
     */
    public function status(){
        $origin_type= $this->request->param('origin_type/d',2);//2收银台3线上点餐
        $goods_id = $this->request->param('goods_id/d',0);
        $status = $this->request->param('status');

        if(!in_array($status,[1,-1]))  return $this->error('状态有误');

        $where['goods_id'] = $goods_id;
        $where['origin_type'] = $origin_type;
        $where['is_delete'] = 0;
        $where['shop_id'] = $this->request->shopId;

        $goods_model = new GoodsModel();
        $goods = $goods_model->getById($where);
        if(empty($goods)) return $this->error('菜品不存在');

        //其他判断后面补充
        if($goods['type'] == 2){
            //判断有没有套餐关联了该产品
            $goods_arr = GoodsGroupDetailModel::where(['goods_id'=>$goods_id,'is_delete'=>0,'shop_id'=>$this->request->shopId])->select()->toArray()??[];
            if($goods_arr && $status<1){
                return $this->error('该产品已被套餐关联，需要删除关联的产品，才能下架');
            }
        }
        $update_str = $origin_type==2?'status':'online_status';
        $update_data[$update_str] = $status;
        GoodsModel::update($update_data,['goods_id'=>$goods_id]);

        return $this->success('操作成功');
    }
    /**
     *删除（产品或套餐、菜单）
     */
    public function del(){
        $goods_id = $this->request->param('goods_id/d',0);

        $where[] = ['origin_type','<>',1];
        $where[] = $del_where[] = ['goods_id','=',$goods_id];
        $where[] = $del_where[] = ['is_delete','=',0];
        $where[] = $del_where[] = ['shop_id','=',$this->request->shopId];

        $goods_model = new GoodsModel();
        $goods = $goods_model->getById($where);
        if(empty($goods)) return $this->error('菜品不存在');

        //判断有没有套餐关联了该产品
        $goods_group_detail = GoodsGroupDetailModel::where(['shop_id'=>$this->request->shopId,'goods_id'=>$goods_id,'is_delete'=>0])->find();

        if($goods_group_detail) return $this->error('该产品已被套餐关联，需要删除关联的产品，才能删除');

        $goods_group_ids = GoodsGroupModel::where(['shop_id'=>$this->request->shopId,'goods_id'=>$goods_id,'is_delete'=>0])->column('goods_group_id');

        GoodsModel::where($del_where)->update(['is_delete'=>1]);
        //产品
        GoodsSkuModel::where($del_where)->update(['is_delete'=>1]);
        //套餐
        GoodsGroupModel::where($del_where)->update(['is_delete'=>1]);
        GoodsGroupDetailModel::where(['shop_id'=>$this->request->shopId,'is_delete'=>0])->whereIn('goods_group_id',$goods_group_ids)->update(['is_delete'=>1]);

        return $this->success('操作成功');

    }
    /**
     * 单独修改会员折扣(未使用，用的之前商品的接口)
     */
    public function saveVipDiscount(){
        $goods_id = $this->request->param('goods_id/d',0);
        $sku_detail = $this->request->param('sku_detail/a',[]);
        $vip_discount_type = $this->request->param('vip_discount_type/d',0);

        $where[] = ['goods_id','=',$goods_id];
        $where[] = ['origin_type','<>',1];
        $where[] = ['is_delete','=',0];
        $where[] = ['shop_id','=',$this->request->shopId];

        if(null === $goods= GoodsModel::where($where)->json(['discount_detail'],true)->find()) return $this->error('菜单不存在');

        if($sku_detail){
            //先修改goods表
            $goods->vip_discount_type = $vip_discount_type;
            if($vip_discount_type>0) $goods->discount_detail = json_encode($sku_detail[0]['discount_detail'],true);
            $goods->save();

            //在修改规格表
            if($vip_discount_type>0 && $goods->is_have_sku){
                $old_sku=GoodsSkuModel::where([

                    ['shop_id','=',$this->request->shopId],

                    ['goods_id','=',$goods->goods_id],

                    ['is_delete','=',0]

                ])->json(['discount_detail'],true)->select()->toArray();

                $old_sku_ids=array_column($old_sku,'sku_id') ?? [];

                foreach ($sku_detail as $k => $v){

                    if(isset($v['sku_id'])){

                        if(!in_array($v['sku_id'],$old_sku_ids)) unset($sku_detail[$k]);

                    }
                    $sku_detail[$k]['discount_detail']=json_encode($v['discount_detail'],true);

                }
                //删除

                $goods_sku_model= new GoodsSkuModel();

                $goods_sku_model->saveAll($sku_detail);
            }


        }

        return $this->success('操作成功');

    }
}

